/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gov.mtc.dgac.licencias.persistence;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import org.springframework.stereotype.Repository;

import pe.gov.mtc.dgac.licencias.bean.AuditorTO;
import pe.gov.mtc.dgac.licencias.bean.AuditorTipoauditTO;
import pe.gov.mtc.dgac.licencias.util.Utiles;

/**
 *
 * @author elvis
 */
@Repository
public class AuditorHibDAO extends HibernateDaoSupport implements AuditorDAO{

    private final static Logger logger = Logger.getLogger(AuditorHibDAO.class);
    
    @Autowired
    public AuditorHibDAO(SessionFactory sessionFactory){
            setHibernateTemplate( new HibernateTemplate(sessionFactory));
    }

        
    public List<AuditorTO> listarAuditores(AuditorTO auditor) {
        DetachedCriteria criteria = DetachedCriteria.forClass(AuditorTO.class);
        logger.debug("listarAuditores()");
        if(auditor !=null){
            if( !Utiles.nullToBlank( auditor.getApePaterno()).equals("")){
                 criteria.add( Restrictions.ilike("apePaterno", auditor.getApePaterno()) );
            }
        }				
        return getHibernateTemplate().findByCriteria(criteria);
    }

    public AuditorTO obtenerAuditor(Integer auditor) {
        Query query = getSession().createQuery(" from AuditorTO a where codAuditor = :id ")
        .setInteger("id", auditor);
        return (AuditorTO) query.uniqueResult();
    }

    public void registrarAuditor(AuditorTO auditor) {
        this.getHibernateTemplate().saveOrUpdate(auditor);
    }

    public void modificarAuditor(AuditorTO auditor) {
        this.getHibernateTemplate().saveOrUpdate(auditor);
    }

    public void eliminarAuditor(String auditor) {
        getSession().createSQLQuery(" delete from  AUDITOR where COD_AUDITOR = :id ")
        .setInteger("id", Integer.parseInt(auditor))
        .executeUpdate();
    }

    public List<AuditorTipoauditTO> listarTipoAuditAuditor(Integer auditor) {
        DetachedCriteria criteria = DetachedCriteria.forClass(AuditorTipoauditTO.class);
        logger.debug("listarAuditores()");
        if( !Utiles.nullToBlank( auditor ).equals("")){
             criteria.add( Restrictions.eq("codAuditor.codAuditor", new Long(auditor) ) );
        }
        return getHibernateTemplate().findByCriteria(criteria);
    }

    public void registrarTipoAuditAuditor(AuditorTipoauditTO audit) {
        getSession().createQuery(" update AuditorTipoauditTO set estado=0 where codAuditor.codAuditor = :id ")
        .setInteger("id", audit.getCodAuditor().getCodAuditor().intValue())
        .executeUpdate();
        this.getHibernateTemplate().saveOrUpdate(audit);
    }

    public void eliminarTipoAuditAuditor(Integer audit) {
         getSession().createSQLQuery(" delete from  AUDITOR_TIPOAUDIT where COD_AUDITOR_TIPO = :id ")
        .setInteger("id", audit)
        .executeUpdate();
    }

    public AuditorTipoauditTO obtenerDetuditor(Integer codDetAudit) {
        Query query = getSession().createQuery(" from AuditorTipoauditTO a where codAuditorTipo = :id ")
        .setInteger("id", codDetAudit);
        return (AuditorTipoauditTO) query.uniqueResult();
    }

}
